let tableIns;
let tree;
let startDateIns;
let endDateIns;
layui.use(['element', 'form', 'table', 'layer', 'laydate', 'tree', 'util'], function () {
    let table = layui.table;
    let form = layui.form;//select、单选、复选等依赖form
    let element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
    let laydate = layui.laydate;
    tree = layui.tree;
    let height = document.documentElement.clientHeight - 60;

    tableIns = table.render({
        elem: '#alreadyListTable'
        , url: ctx + '/agent/cms/alreadylist/page'
        , method: 'POST'
        //请求前参数处理
        , request: {
            pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'rows' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusName: 'flag' //规定数据状态的字段名称，默认：code
            , statusCode: true //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: 'records' //规定数据总数的字段名称，默认：count
            , dataName: 'rows' //规定数据列表的字段名称，默认：data
        }
        //响应后数据处理
        , parseData: function (res) { //res 即为原始返回的数据
            var data = res.data;
            return {
                "flag": res.flag, //解析接口状态
                "msg": res.msg, //解析提示文本
                "records": data.records, //解析数据长度
                "rows": data.rows //解析数据列表
            };
        }
        , toolbar: '#alreadyListTableToolbar'
        , title: '代理商分润清单'
        , cols: [[
            {field: 'serial', width:200, title: '结算单号'}
            , {field: 'sellerName',title: '商户名称'}
            , {field: 'agentName',title: '代理商名称'}
            , {field: 'transAmount',title: '发放金额', align:'right', style:'font-weight:bold',
            	templet: function(d){return formatMoney(d.transAmount);}}
            , {field: 'transType',title: '分润类型'}
            , {field: 'stepAmount',title: '分润金额', align:'right', style:'font-weight:bold',
            	templet: function(d){return formatMoney(d.stepAmount);}}
            , {field: 'createTime', width:200, title: '分润时间'}
        ]]
        , defaultToolbar: ['', '', '']
        , page: true
        , height: height
        , cellMinWidth: 80
    });

    //头工具栏事件
    table.on('toolbar(display)', function (obj) {
        switch (obj.event) {
            case 'query':
                let queryBySellerName = $("#queryBySellerName").val();
                let queryByAgentName = $("#queryByAgentName").val();
                let queryByStartDate = $("#queryByStartDate").val();
                let queryByEndDate = $("#queryByEndDate").val();
                
                table.reload('alreadyListTable', {
                    url: ctx + '/agent/cms/alreadylist/page'
                    , where: {
                    	sellerName: queryBySellerName
           			 , agentName: queryByAgentName
           			 , startDate: queryByStartDate
           			 , endDate: queryByEndDate
                    }
                    , page: {
                        curr: 1
                    }
                });
                
                $("#queryBySellerName").val(queryBySellerName);
                $("#queryByAgentName").val(queryByAgentName);
                $("#queryByStartDate").val(queryByStartDate);
                $("#queryByEndDate").val(queryByEndDate);
                
                //日期选择器
                laydate.render({
                    elem: '#queryByStartDate',
                    format: "yyyy-MM-dd",
                    trigger: 'click'
                });
                
            	//日期选择器
                laydate.render({
                    elem: '#queryByEndDate',
                    format: "yyyy-MM-dd",
                    trigger: 'click'
                });
                break;
        }
    });

    //日期选择器
    startDateIns = laydate.render({
        elem: '#queryByStartDate',
        format: "yyyy-MM-dd",
        trigger: 'click'
    });
    
	//日期选择器
    endDateIns = laydate.render({
        elem: '#queryByEndDate',
        format: "yyyy-MM-dd",
        trigger: 'click'
    });
});

